package com.example.administrator.service;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    con servicicon=new con();
    private MyService.mybinder binder;
    private static final String TAG="MainActivity";
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        progressBar= (ProgressBar) this.findViewById(R.id.jdt);

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction("bfq");
        MyReciver myReciver = new MyReciver();
        registerReceiver(myReciver,intentFilter);
    }

    public void djsj(View v){
        int id=v.getId();
        switch (id){
            case R.id.start:
                Intent intent=new Intent();
                intent.setClass(this,MyService.class);
                startService(intent);
                break;
            case R.id.stop:
                Intent a=new Intent();
                a.setClass(this,MyService.class);
                stopService(a);
                break;
            case R.id.bind:
                Intent b=new Intent();
                b.setClass(this,MyService.class);
                bindService(b,servicicon,BIND_AUTO_CREATE);
                break;
            case R.id.unbind:
                unbindService(servicicon);
                break;
            case R.id.play:
                binder.play();
                break;
            case R.id.pause:
                binder.pause();
                break;
            case R.id.end:
                binder.end();
                break;
        }



    }
    class con implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder = (MyService.mybinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    class MyReciver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            int duration = intent.getIntExtra("duration",0);
            int currentPosition = intent.getIntExtra("currentPosition",0);
            Log.e(TAG, "onReceive: "+currentPosition+"-----"+duration);
            progressBar.setMax(duration);
            progressBar.setProgress(currentPosition);
        }
    }

}
